package Theard;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.print.attribute.standard.Severity;
import javax.swing.*;

public class BaiTongHop extends JFrame{

	JButton b1 = new JButton("20 số nguyên ngẫu nhiên"),
			b2 = new JButton("Số chẵn"),
			b3 = new JButton("Số lẽ"),
			b4 = new JButton("20 số nguyên tố"),
			b5 = new JButton("20 số Fibonacci"),
			b6 = new JButton("Thoát");
	JTextField[] 	NNN = new JTextField[20],
	           		SNT = new JTextField[20],
					FIB = new JTextField[20];
	JPanel 	soNN = new JPanel(),
			soNT = new JPanel(),
			soFib = new JPanel();
	int[] nNgauNhien = new int[20];
	
	public BaiTongHop(){
		setSize(900, 400);
		setVisible(true);	

		soNN.setLayout(new GridLayout(1,20));
		for(int i =0; i < 20; i++){
			NNN[i] = new JTextField();
			NNN[i].setHorizontalAlignment(JTextField.CENTER);
			soNN.add(NNN[i]);
		}


		soNT.setLayout(new GridLayout(1,20));
		for(int i =0; i < 20; i++){
			SNT[i] = new JTextField();
			SNT[i].setHorizontalAlignment(JTextField.CENTER);
			soNT.add(SNT[i]);
		}

		soFib.setLayout(new GridLayout(1,20));
		for(int i =0; i < 20; i++){
			FIB[i] = new JTextField();
			FIB[i].setHorizontalAlignment(JTextField.CENTER);
			soFib.add(FIB[i]);
		}
		
		JPanel ct = new JPanel();
		ct.setLayout(new GridLayout(6,1));
		ct.add(new JLabel("20 số ngẫu nhiên"));
		ct.add(soNN);
		ct.add(new JLabel("20 số nnguyên tố đầu tiên"));
		ct.add(soNT);
		ct.add(new JLabel("20 số Fibonacci đầu tiên"));
		ct.add(soFib);
		
		add(ct, BorderLayout.CENTER);
		
		JPanel nut = new JPanel();
		nut.add(b1); nut.add(b2); nut.add(b3);
		nut.add(b4); nut.add(b5); nut.add(b6);
		add(nut, BorderLayout.SOUTH);
		
		b1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				for(int i = 0; i < 20; i++){
					nNgauNhien[i] = (int)(100*Math.random());
					NNN[i].setText(String.valueOf(nNgauNhien[i]));
					NNN[i].setBackground(Color.WHITE);
					NNN[i].setForeground(Color.BLACK);
				}
			}
		});
		
		b2.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				for(int i = 0; i < 20; i++){
					if(nNgauNhien[i]%2 == 0){
						NNN[i].setBackground(Color.CYAN);
						NNN[i].setForeground(Color.RED);
					}
					else {
						NNN[i].setBackground(Color.WHITE);
						NNN[i].setForeground(Color.BLACK);
					}
				}
			}
		});
		
		b3.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				for(int i = 0; i < 20; i++){
					if(nNgauNhien[i]%2 != 0)
						NNN[i].setBackground(Color.GREEN);
					else NNN[i].setBackground(Color.WHITE);
				}
			}
		});
		
		b4.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				SoNguyenTo();
			}
		});
		
		b5.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				long[] a = new long[20];
				a[0] = 0; a[1] = 1;
				FIB[0].setText(String.valueOf(a[0]));
				FIB[1].setText(String.valueOf(a[1]));
				for(int i = 2; i < 20; i++){
					a[i] = a[i-1] + a[i-2];
					FIB[i].setText(String.valueOf(a[i]));
				}
			}
		});
		
		b6.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				System.exit(0);
			}
		});
	}
	
	public static boolean IsNguyenTo(long n){
		boolean resuft = true;
		if(n >= 2){
			for(int i = 2; i < n; i++){
				if(n % i == 0) resuft = false;
			}
		}
		else resuft = false;
		return resuft;
	}	
	public void SoNguyenTo(){
		int[] a = new int[20];
		int i = 0;
		int n = 0;
		do {
			if(IsNguyenTo(n)){
				a[i] = n;
				i++;
			}
			n++;
		}
		while(i < 20);		
		for(int j = 0; j < 20; j++){
			SNT[j].setText(String.valueOf(a[j]));
		}
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new BaiTongHop();
	}

}
